package org.fs;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.fs.entities.Flight;

public class FlightWrapper {

	private Flight wrapped;
	private static SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
	private static SimpleDateFormat daytimeFormat = new SimpleDateFormat("yyyyMMddHHmm");
	private static SimpleDateFormat displayFormat = new SimpleDateFormat("dd-MM-yyyy");
	private static SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
	
	private static AirportAbbreviationMap aam;

	public FlightWrapper(){
		wrapped = new Flight();
		wrapped.setCheckDate(new Date());
	}
	
	public String getDay() {
		return displayFormat.format(wrapped.getFlightDate());
	}
	public void setDay(String day) {
		Date date = null;
		try {
			date = format.parse(day);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		wrapped.setFlightDate(date);
		
	}

	public String getPrice() {
		return wrapped.getPrice().toString();
	}
	public void setPrice(String price) {
		BigDecimal number = new BigDecimal(price);
		wrapped.setPrice(number);
	}
	public String getId() {
		return wrapped.getCode();
	}
	public void setId(String id) {
		wrapped.setCode(id);
		
	}
	public String getDeparture() {
		return wrapped.getDeparture().toString();
	}
	public void setDeparture(String departure) {
		Date date = null;
		try {
			date = daytimeFormat.parse(departure);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		wrapped.setDeparture(timeFormat.format(date));
	}
	public String getArrival() {
		return wrapped.getArrival().toString();
	}
	public void setArrival(String arrival) {
		Date date = null;
		try {
			date = daytimeFormat.parse(arrival);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		wrapped.setArrival(timeFormat.format(date));
	}
	
	@Override
	public String toString() {
		return "Day: " + getDay()  + " Price: " + getPrice();
	}
	public String getTo() {
		return wrapped.getAirportByFromAirportId().getAbbreviation();
	}
	public void setTo(String to) {
		wrapped.setAirportByToAirportId(aam.get(to));
	}
	public String getFrom() {
		return wrapped.getAirportByToAirportId().getAbbreviation();
	}
	public void setFrom(String from) {
		wrapped.setAirportByFromAirportId(aam.get(from));
	}
	public Flight getWrapped() {
		return wrapped;
	}
	public void setWrapped(Flight wrapped) {
		this.wrapped = wrapped;
	}

	public static AirportAbbreviationMap getAam() {
		return aam;
	}

	public static void setAam(AirportAbbreviationMap aam) {
		FlightWrapper.aam = aam;
	}
	
	
}
